package com.kun.interceptor;

import com.kun.entity.SysUser;
import com.kun.util.LoginUserContext;
import com.kun.util.LoginUserUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author kun.li
 */
public class LoginUserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 只要拦截到就先清理,避免上下文污染
        LoginUserContext.clear();
        SysUser loginUser = LoginUserUtils.getLoginUser(request, false);
        LoginUserContext.setUser(loginUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        LoginUserContext.clear();
    }
}
